﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TemplateManager.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the TemplateManager class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.BusinessLogic
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the TemplateManager class.
    /// </summary>
    public static class TemplateManager
    {
        /// <summary>
        /// Initializes a new instance of the Template class.
        /// </summary>
        /// <param name="templateId">The identity of the database table's row referencing this template.</param>
        /// <returns>An instantiated template if found, null if not.</returns>
        public static BusinessObjects.Template GetTemplate(int templateId)
        {
            return Data.Template.Select(templateId);
        }
        
        /// <summary>
        /// Gets a dictionary of templates for the current application (determined by the connection's sql user account.)
        /// </summary>
        /// <param name="templateType">The type of templates to return (Header or Footer).</param>
        /// <returns>A dictionary object containing row identity keys and the referenced template's name.</returns>
        public static Dictionary<int, string> GetTemplates(TemplateArea templateType)
        {
            List<BusinessObjects.Template> templates = Data.Template.SelectAllWithoutText(templateType);
            Dictionary<int, string> ts = new Dictionary<int, string>();
            for (int i = 0; i < templates.Count; i++)
            {
                ts.Add(templates[i].Id, templates[i].Name);
            }

            return ts;
        }
        
        /// <summary>
        /// Gets a dictionary of templates for the current application (determined by the connection's sql user account.)
        /// </summary>
        /// <param name="templateType">The type of templates to return (Header or Footer).</param>
        /// <returns>A dictionary object containing row identity keys and the referenced template's name.</returns>
        public static List<BusinessObjects.Template> GetTemplatesWithoutText(TemplateArea templateType)
        {
            return Data.Template.SelectAllWithoutText(templateType);
        }

        /// <summary>
        /// Gets the titles of pages related to this template.
        /// </summary>
        /// <returns>A readonly list of the titles of the pages referencing this template.</returns>
        public static List<string> GetPages(BusinessObjects.Template template)
        {
            return Data.PageLayout.SelectAllPageNames(template);
        }

        /// <summary>
        /// Inserts the Template object into the database.
        /// </summary>
        /// <returns>The inserted Template with a newly populated Id field.</returns>
        public static BusinessObjects.Template SaveAs(BusinessObjects.Template template)
        {
            return Data.Template.Insert(template);
        }

        /// <summary>
        /// Updates the Template's database fields.
        /// </summary>
        public static void Save(BusinessObjects.Template template)
        {
            Data.Template.Update(template);
        }

        /// <summary>
        /// Deletes the Template's database entry.
        /// </summary>
        public static void Delete(BusinessObjects.Template template)
        {
            Data.Template.Delete(template);
        }
    }
}